window.addEventListener("load", function() { rememberExtension.init(); }, false);

// Create an array of sites that can be pushed to sortAndDisplay
function makeTheseSites(uri, title, first, last, count, theseSites) {
  var justnow = Date.now();

  if (first == last) {
    rank = "00000000";
  } else {
    var average = (last - first) / count;
    var sincelast = justnow - last;
    var rank = (sincelast / average) * 1000;
    
    rank = Math.round(rank);
    
    // THIS REALLY SUCKS!
    if (rank < 10) {
      rank = "0000000" + rank;
    } else if (rank < 100) {
      rank = "000000" + rank;
    } else if (rank < 1000) {
      rank = "00000" + rank;
    } else if (rank < 10000) {
      rank = "0000" + rank;
    } else if (rank < 100000) {
      rank = "000" + rank;
    } else if (rank < 1000000) {
      rank = "00" + rank;
    } else if (rank < 10000000) {
      rank = "0" + rank;
    }
  }  
  var currently = new Array(rank, uri, title, count, first, last);
  theseSites.push(currently);
  
  return theseSites;
}

// Expects an array of sites, used in doVooDoo() to display everything
function sortAndDisplay(theseSites) {
  theseSites = theseSites.sort();
  theseSites = theseSites.reverse();
  
  var everything = "";

  // This could be moved elsewhere
  function addLeftPadding(value) {
    if (value < 10) {
      value = "0" + value;
    }
    return value;
  }
  
  // This could be moved to the view!  
  function formatDate(timestamp) {
    First = new Date(timestamp);
    return First.getFullYear() + "/" + addLeftPadding((First.getMonth()+1)) + "/" + addLeftPadding(First.getDate()) + " @ " + addLeftPadding(First.getHours()) + ":" + addLeftPadding(First.getMinutes());
  }
  
  for (var key in theseSites) {
    if (theseSites[key][2] == null) {
      var pagetitle = "Untitled document";
    } else {
      var pagetitle = theseSites[key][2];
    }
    
    everything = everything + "<tr><td><b>" + theseSites[key][0] + "</b></td><td>"+ theseSites[key][3] + "</td><td><a href='" + theseSites[key][1] + "'>" + pagetitle + "</a></td><td>" + formatDate(theseSites[key][5]) + "</td><td>" + formatDate(theseSites[key][4]) + "</td></tr>\n";
  }
  
  var header = "<html>\n<head>\n<title>Remember To Surf!</title>\n</head>\n<body>\n<table border='1' width='100%'><tr><td>Rank</td><td>Visits</td><td>Site</td><td>Last visit</td><td>Added</td></tr>\n";
  var footer = "</table>\n</body>\n</html>";
  
  everything = header + everything + footer;
 
  var savefile = Components.classes["@mozilla.org/file/directory_service;1"]
    .getService(Components.interfaces.nsIProperties)
    .get("ProfD", Components.interfaces.nsIFile);
  savefile.append("remembertosurf.html");
  
  var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
    .createInstance(Components.interfaces.nsIFileOutputStream);
  foStream.init(savefile, 0x02 | 0x08 | 0x20, 0777, 0);
  foStream.write(everything, everything.length);
  foStream.close();

  gBrowser.selectedTab = gBrowser.addTab(savefile.path);
}